/*
 * StartScreen.cpp
 *
 *  Created on: 2015-9-24
 *      Author: 10095427
 */

#include "StartScreen.h"
#include "TitleMenuItem.h"
#include "MapScreen.h"
#include "LocalizedStringGetter.h"
#include "StoryConfig.h"
#include "Story1Layer.h"

#define BUTTON_WIDTH 135
#define BUTTON_HEIGHT 50
#define BUTTON_Y      150
#define TITLE_TOP_PAD 300
#define TITLE_WIDTH   320

typedef enum {
    TagMenu = 1,
    TagPlay,


} MenuTag;

typedef enum {
    ZBg,
    ZTitle,
    ZMenu,


} ZIndex;

StartScreen::StartScreen() {

}

StartScreen::~StartScreen() {
}

Scene *StartScreen::createScene()
{
    auto scene = Scene::create();
    auto layer = StartScreen::create();
    scene->addChild(layer);
    return scene;
}

bool StartScreen::init()
{
    if (!Layer::init()) {
        return false;
    }
    bgInit();
    menuInit();
    titleInit();
    return true;
}

void StartScreen::titleInit()
{
    Size winsize = Director::getInstance()->getWinSize();
    auto title = Sprite::create(LocalizedStringGetter::getStringByKey("StartTitleName"));
    title->setAnchorPoint(Vec2::ZERO);
    title->setPosition(Point((winsize.width-TITLE_WIDTH)/2,winsize.height - TITLE_TOP_PAD));
    addChild(title,ZTitle);
}

void StartScreen::menuInit()
{
    Size winsize = Director::getInstance()->getWinSize();
    auto itemPlay = TitleMenuItem::create("WinButtonBg.png", "WinButtonSelected.png","WinButtonBg.png", CC_CALLBACK_1(StartScreen::menuCallback, this));
    itemPlay->setAnchorPoint(Vec2::ZERO);
    itemPlay->setTag(TagPlay);
    itemPlay->setTitle(LocalizedStringGetter::getStringByKey("StartGame"), Size(BUTTON_WIDTH,BUTTON_HEIGHT));
    itemPlay->setPosition(Point((winsize.width-BUTTON_WIDTH)/2,BUTTON_Y));

    auto menu = Menu::create(itemPlay, NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu,ZMenu);
}

void StartScreen::menuCallback(Ref *sender)
{
    TitleMenuItem *item = (TitleMenuItem*)sender;
    if(item->getTag() == TagPlay){
    	if(StoryConfig::getInstance()->isStoryShowed(StoryConfigType1)){
    		Director::getInstance()->pushScene(MapScreen::createScene());
    	}else{
    		Director::getInstance()->pushScene(Story1Layer::createScene());
    	}
    }
}


void StartScreen::bgInit()
{
    auto bg = Sprite::create("StartBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    addChild(bg,ZBg);
}

